<?php
/**
 * DESCRIPTION:
 *  class that generates a html form
 *  in this version, only forms with table-based markup are supported
 * FIELDS:
 *  public $fields is an array of form's elements
 *  protected $submit is for Submit button caption
 *  protected $td_class is a class for td
 *  protected $td_style is a css sytle for td
 *  protected $field_class is a class for field
 *  protected $field_style is a css style for field
 * METHODS:
 *  public void __construct($fields, $submit, $td_class="", $td_style="", $field_class="", $field_style="") is a class constructor
 *  public void print_form() generates HTML output
 *  public void __toString() overrides std function
 *  public array check() validates data in fields
 * ======
 * @author at8eqeq3
 */
class form {
    public $fields;
    protected $submit;
    protected $td_class;
    protected $td_style;
    protected $field_class;
    protected $field_style;

    public function __construct($fields, $submit, $td_class = "", $td_style = "", $field_class = "", $field_style = ""){
        $this->fields = $fields;
        $this->submit = $submit;
        $this->td_class = $td_class;
        $this->td_style = $td_style;
        $this->field_class = $field_class;
        $this->field_style = $field_style;

        foreach($fields as $key => $field){
            if(!is_subclass_of($field, 'field')){
                throw new ExceptionObject($key, '<b>"'.$key.'"</b> is not a form field');
            }
        }
    }

    public function print_form(){
        $enctype = '';
        //applying classes and styles to fields, defining content-type
        if(!empty($this->fields)){
            foreach($this->fields as $field){
                if(!empty($this->field_class)){
                    $field->class = $this->field_class;
                }
                if(!empty($this->field_style)){
                    $field->style = $this->field_style;
                }
                if($field->type == 'file'){
                    $enctype = 'enctype="multipart/form-data"';
                }
            }
        }

        //applying classes and styles to td's
        if(!empty($this->td_class)){
            $class = 'class="'.$this->td_class.'"';
        }else{
            $class = '';
        }
        if(!empty($this->td_style)){
            $style = 'style="'.$this->td_style.'"';
        }else{
            $style = '';
        }
        //now it's time to put something out
        echo "<!-- form output started -->\n"; //comment this out, if no need
        echo "<form name=\"form\" $enctype method=\"post\">\n";
        echo "<table>\n";
        if(!empty($this->fields)){
            foreach($this->fields as $field){
                list($caption, $tag, $help, $alternative) = $field->get_html();
                if(is_array($tag)){
                    $tag = implode("<br />", $tag);
                }
                echo "<tr>\n";
                echo "  <td width=\"100\" $style $class>$caption:</td>\n";
                echo "  <td $style $class>$tag</td>\n";
                echo "</tr>\n";
                if(!empty($help)){
                    echo "<tr>\n";
                    echo "  <td>&nbsp;</td>\n";
                    echo "  <td $style $class>$help</td>\n";
                    echo "</tr>\n";
                }
            }
            echo "<tr>\n";
            echo "  <td $style $class>&nbsp;</td>\n";
            echo "  <td $style $class><input class=\"button\" type=\"submit\" value=".htmlspecialchars($this->submit, ENT_QUOTES)."></td>\n";
            echo "</tr>\n";
        }
        echo "</table>\n";
        echo "</form>\n";
        echo "<!-- form output finished -->\n"; //comment this out, if no need
    }

    public function __toString(){
        $this->print_form();
    }

    public function check(){
        $arr = array();
        if(!empty($this->fields)){
            foreach($this->fields as $field){
                $str = $field->check();
                if(!empty($str)){
                    $arr[] = $str;
                }
            }
        }
        return $arr;
    }
}
?>